package org.ihelpuoo.orm.handler;

import java.util.Iterator;

import org.ihelpuoo.orm.FieldRule;
import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.orm.IIIManual;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;

public abstract class MicroObjectProcessor extends ObjectProcessor
{

	public MicroObjectProcessor(IIIManual manual)
	{
		super(manual);
	}

	/**
	 * It will fetch a bean detail information from a Document object.
	 * Bean detail information is supposed to store as the Document root 
	 * element, and it should according the BeanRule.XMLRule's defination.
	 * If can not found the defination, it will consider the Element as
	 * it's default rule
	 *  
	 * @see www.w3c.org
	 * @see org.ihelpuoo.orm.Bean
	 * @see org.ihelpuoo.orm.rule.BeanRule
	 * @see org.ihelpuoo.orm.filter.FieldFilter
	 * @param br: BeanRule
	 * @param doc: the Document object
	 * @param actived: Which fields should be highlight.
	 * @param locked: Which fileds shoudl be ignore.
	 * @param bean: original bean
	 * @return Bean object
	 * @throws IIIException
	 */
	@Override
	public Object fetch(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked, String prefix) throws IIIException
	{
		Iterator<FieldRule> it = r.rules().iterator();
		while (it.hasNext())
		{
			FieldRule fr = it.next();
			if (fr.isIgnore(actived,locked))
				continue;
			getFieldHandlers.process(r,fr.getField(),src,obj,prefix);
		}
		return obj;
	}

	/**
	 * Update the (Element)src by given <b>bean</b> object.
	 * 
	 * @param src: a Element object.
	 */
	@Override
	public Object update(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked) throws IIIException
	{
		Iterator<FieldRule> it = r.rules().iterator();
		while (it.hasNext())
		{
			FieldRule fr = it.next();
			if (fr.isIgnore(actived,locked))
				continue;
			setFieldHandlers.process(r,fr.getField(),src,obj,null);
		}
		return obj;
	}

}
